import { _decorator, Component, EditBox, Enum, Sprite } from 'cc';
import { StringUtil } from '../../Util/StringUtil';
const { ccclass, property, menu } = _decorator;

/**展示类型 */
enum EditType {
  /**可编辑 */
  CHANGE,
  /**只读 */
  READONLY,
}

@ccclass('EditCount')
@menu('myComponent/EditCount')
export class EditCount extends Component {
  /**展示类型 */
  @property({
    type: Enum(EditType),
    tooltip: '展示类型',
  })
  editType: EditType = EditType.READONLY;
  @property({
    type: EditBox,
  })
  editBox: EditBox;
  @property({
    visible() {
      return this.editType == EditType.CHANGE;
    },
  })
  reg: string = '';
  @property
  labelSub: string = '/';

  isEnabled: boolean = true;
  private count: number = 1;
  private maxCount: number = 0;

  //自定义输入数据判断 'max'->最大值 'min'->最小值
  inputFunc: (val: string) => number;
  callBack: (val: number) => void;

  onLoad(): void {
    switch (this.editType) {
      case EditType.CHANGE:
        this.editBox.enabled = true;
        break;
      case EditType.READONLY:
        this.editBox.enabled = false;
        break;
    }
  }

  setEnabled(boo: boolean) {
    this.isEnabled = boo;
    let btn = this.node.getChildByName('add');
    btn.getComponent(Sprite).grayscale = !boo;
    btn = this.node.getChildByName('max');
    btn.getComponent(Sprite).grayscale = !boo;
    btn = this.node.getChildByName('min');
    btn.getComponent(Sprite).grayscale = !boo;
    btn = this.node.getChildByName('reduce');
    btn.getComponent(Sprite).grayscale = !boo;
    this.editBox.enabled = boo && this.editType == EditType.CHANGE;
  }

  setCount(num: number) {
    this.count = num;
    let str: string = '';
    if (this.maxCount > 0) {
      str = this.labelSub + this.maxCount;
    }
    this.editBox.string = StringUtil.numberToString(this.count) + str;

    if (this.callBack != null) this.callBack(this.count);
    let boo = this.maxCount > 0 && this.count >= this.maxCount;
    let btn = this.node.getChildByName('add');
    btn.getComponent(Sprite).grayscale = boo;
    btn = this.node.getChildByName('max');
    btn.getComponent(Sprite).grayscale = boo;
    boo = this.count <= 1;
    btn = this.node.getChildByName('min');
    btn.getComponent(Sprite).grayscale = boo;
    btn = this.node.getChildByName('reduce');
    btn.getComponent(Sprite).grayscale = boo;
  }

  setMaxCount(num: number, max: number) {
    this.maxCount = max;
    if (num > max) {
      num = max;
    }
    this.setCount(num);
  }

  getCount(): number {
    return this.count;
  }

  /** 数量控制 */
  onBtnChange(tag, val) {
    if (val == 'max' || val == 'min' || isNaN(Number(val))) return;
    // AudioManager.I.playSound(AudioManager.I.getClick2Code());
    if (this.inputFunc == null) {
      if (val == 'max') {
        if (this.maxCount > 0) {
          this.setCount(this.maxCount);
        }
      } else if (val == 'min') {
        this.setCount(1);
      } else {
        const num = this.count + Number(val);
        if (num <= 0) {
          this.setCount(1);
        } else if (num > this.maxCount) {
          this.setCount(this.maxCount);
        } else {
          this.setCount(num);
        }
      }
    } else {
      this.setCount(this.inputFunc(val));
    }
  }

  /** 绑定输入文本限制输入 */
  onEditBoxChanged(editbox: EditBox) {
    editbox.blur();
    let str = '';
    const reg = new RegExp(this.reg);
    for (let i = 0; i < editbox.string.length; i++) {
      if (reg.test(editbox.string.charAt(i))) {
        str += editbox.string.charAt(i);
      }
    }
    editbox.string = str;
    editbox.focus();
  }
}
